package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.utils.Assert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ATrain;
import com.ruoyi.system.domain.AUser;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.vo.HgATrainInforVO;
import com.ruoyi.system.domain.vo.XcxATeamInfoVO;
import com.ruoyi.system.domain.vo.XcxATrainInforVO;
import com.ruoyi.system.mapper.AUserMapper;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.util.LoginUtils;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ATrainInforMapper;
import com.ruoyi.system.domain.ATrainInfor;
import com.ruoyi.system.service.IATrainInforService;

/**
 * 资料类别Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-29
 */
@Service
public class ATrainInforServiceImpl implements IATrainInforService {
    @Autowired
    private ATrainInforMapper aTrainInforMapper;

    @Autowired
    private AUserMapper aUserMapper;

    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * 查询资料类别
     *
     * @param id 资料类别主键
     * @return 资料类别
     */
    @Override
    public ATrainInfor selectATrainInforById(Long id) {
        return aTrainInforMapper.selectATrainInforById(id);
    }

    /**
     * 查询资料类别列表
     *
     * @param hgATrainInforVO 资料类别
     * @return 资料类别
     */
    @Override
    public List<HgATrainInforVO> selectATrainInforList(HgATrainInforVO hgATrainInforVO) {
        return aTrainInforMapper.selectATrainInforList(hgATrainInforVO);
    }

    /**
     * 新增资料类别
     *
     * @param aTrainInfor 资料类别
     * @return 结果
     */
    @Override
    public int insertATrainInfor(ATrainInfor aTrainInfor) {
        aTrainInfor.setCreateTime(DateUtils.getNowDate());
        return aTrainInforMapper.insertATrainInfor(aTrainInfor);
    }

    /**
     * 修改资料类别
     *
     * @param aTrainInfor 资料类别
     * @return 结果
     */
    @Override
    public int updateATrainInfor(ATrainInfor aTrainInfor) {
        aTrainInfor.setUpdateTime(DateUtils.getNowDate());
        return aTrainInforMapper.updateATrainInfor(aTrainInfor);
    }

    /**
     * 批量删除资料类别
     *
     * @param ids 需要删除的资料类别主键
     * @return 结果
     */
    @Override
    public int deleteATrainInforByIds(Long[] ids) {
        return aTrainInforMapper.deleteATrainInforByIds(ids);
    }

    /**
     * 删除资料类别信息
     *
     * @param id 资料类别主键
     * @return 结果
     */
    @Override
    public int deleteATrainInforById(Long id) {
        return aTrainInforMapper.deleteATrainInforById(id);
    }

    @Override
    public List<HgATrainInforVO> selectATrainInforXcxList(HgATrainInforVO hgATrainInforVO) {
        AUser au = aUserMapper.selectAUserById(LoginUtils.getUser().getId());
        hgATrainInforVO.setAgentLevel(au.getRole());
        List<HgATrainInforVO> list = aTrainInforMapper.selectATrainInforXcxList(hgATrainInforVO);
        for (HgATrainInforVO at : list) {
            if (StringUtils.isNotEmpty(at.getTrainWord())) {
                at.setIsWord(true);
            } else {
                at.setIsWord(false);
            }
        }
        return list;
    }

    @Override
    public XcxATrainInforVO selectATrainInforXcxById(Long id) {
        Long userId = LoginUtils.getUser().getId();
        return aTrainInforMapper.selectATrainInforXcxById(id, userId);
    }

    @Override
    public int checkWord(ATrainInfor aTrainInfor) {
        Assert.notNull(aTrainInfor.getId(), "当前数据id不能为空！");
        Assert.notNull(aTrainInfor.getTrainWord(), "密码不能为空！");
        ATrainInfor infor = aTrainInforMapper.checkWord(aTrainInfor);
        return infor != null ? 1 : 0;
    }
}
